const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')

module.exports = {
  mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
  entry: {
    list: './src/list/index.js',
    detail: './src/detail/index.js',
  },
  output: {
    filename: 'script/[name].[chunkhash:5].js',
  },
  plugins: [
    new CleanWebpackPlugin(),

    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, './public/list.html'),
      filename: 'list.html', // 生成html文件的名称
      chunks: ['list'], // 有多个chunk时，引入自己的chunk js文件
    }),

    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, './public/detail.html'),
      filename: 'detail.html', // 生成html文件的名称
      chunks: ['detail'], // 有多个chunk时，引入自己的chunk js文件
    }),

    new CopyWebpackPlugin({
      patterns: [
        { from: './public/css', to: './css' },
        { from: './public/img', to: './img' },
      ],
    }),
  ],

  devServer: {
    open: {
      target: ['list.html'], // 默认打开的页面
    },
    proxy: {
      '/api': {
        target: 'http://yuanjin.tech:5100',
        changeOrigin: true,
      },
    },
  },
  stats: {
    modules: false,
  },
}
